//#define __DEBUG__
#define THIS_FILE "fn_subselect.sqf"
#include "x_setup.sqf"

/************************************************************
	Subarray Select
	By Andrew Barron

Returns a sub-selection of the passed array. There are various
methods the sub-array can be determined.

Parameters: [array, beg, <end>]
Returns: subarray

array - Array to select sub array from.
beg - Index of array to begin sub-array. If negative, index is
	counted from the end of array.
end - Optional. Index of array to end the sub-array. If ommitted,
	remainder of the array will be selected. If negative, it
	specifies the length of the sub-array (in absolute form).

Examples:

	_array = ["a","b",true,3,8];
	[_array, 2] call FUNC(subSelect); //returns [true,3,8]
	[_array, -2] call FUNC(subSelect); //returns [3,8]
	[_array, 1, 3] call FUNC(subSelect); //returns ["b",true,3]
	[_array, 1, -2] call FUNC(subSelect); //returns ["b",true]

************************************************************/

private ["_ary","_beg","_len","_end","_ret","_i"];
PARAMS_2(_ary,_beg);
_ret = [];
_len = (count _ary)-1;
if (_beg < 0) then {_beg = _len + _beg + 1};
_end = if (count _this > 2) then {_this select 2} else {_len};
if (_end < 0) then {_end = _beg - _end - 1};
if (_end > _len) then {_end = _len};
for "_i" from _beg to _end do {_ret set [count _ret, _ary select _i]};
_ret